<?php
/**
 * Created by PhpStorm.
 * User: dy
 * Date: 2020/1/2
 * Time: 17:00
 */
namespace app\admin\controller;
use app\admin\model\AdminActionLogModel;
use app\common\controller\BackendController;
use app\admin\model\Recycle as RecycleModel;
use think\Exception;
use think\facade\Db;

class Recycle extends BackendController
{
    /**
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index(){
        if($this->request->isAjax()){
            $this->model = RecycleModel::instance();
            list($where,$sort,$order,$page,$limit) = $this->model->buildParams($this->request);
            $total = $this->model
                ->where($where)
                ->count();
            $list = $this->model
                ->where($where)
                ->page($page,$limit)
                ->order($sort,$order)
                ->select();
            $this->success('查询成功','',['list'=>$list,'total'=>$total]);
        }
        return view();
    }
    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delete(){
        $ids = $this->request->param('ids/a');
        if(empty($ids)){
            $this->error('删除的数据不能为空！');
        }
        $recycle_list = RecycleModel::select($ids);
        $delete_data = [];
        foreach ($recycle_list as $item){
            if(!isset($delete_data[$item['table_name']])){
                $delete_data[$item['table_name']] = [];
            }
            $delete_data[$item['table_name']][] = $item['object_id'];
        }
        try {
            Db::transaction(function ()use ($delete_data,$ids){
                foreach ($delete_data as $table_name => $table_ids) {
                    $res = Db::name($table_name)->delete($table_ids);
                    if(!$res){
                        throw new Exception('删除失败,表名：'.$table_name);
                    }
                }
                $delete_res = RecycleModel::destroy($ids,true);
                if(!$delete_res){
                    throw new Exception('删除失败',401);
                }
            });
            AdminActionLogModel::logAdd('删除回收站','ids:'.json_encode($ids));
            $this->success('删除成功！');
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }
    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function restore(){
        $ids = $this->request->param('ids/a');
        if(empty($ids)){
            $this->error('被恢复的数据不能为空！');
        }
        $recycle_list = RecycleModel::select($ids);
        $delete_data = [];
        foreach ($recycle_list as $item){
            if(!isset($delete_data[$item['table_name']])){
                $delete_data[$item['table_name']] = [];
            }
            $delete_data[$item['table_name']][] = $item['object_id'];
        }
        try {
            Db::transaction(function ()use ($delete_data,$ids){
                foreach ($delete_data as $table_name => $table_ids) {
                    $res = Db::name($table_name)->whereIn('id',$table_ids)->update(['delete_time'=>DATA_DISABLE]);
                    if(!$res){
                        throw new Exception('恢复失败,表名：'.$table_name);
                    }
                }
                $delete_res = RecycleModel::destroy($ids,true);
                if(!$delete_res){
                    throw new Exception('恢复失败',401);
                }
            });
            AdminActionLogModel::logAdd('恢复回收站数据','ids:'.json_encode($ids));
            $this->success('恢复成功！');
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }

}